<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Prosoccer_parser  extends CI_Controller{
	
	public function __construct()
	{
		parent::__construct();
		// imports
		//$this->load->model('game');
		$this->load->library('factory');
		$this->factory->create('GameTO');
		$this->load->library('simple_html_dom');
		
	}
	// parse teams
	public function parseTeams($teamString)
	{
		$teams = explode(" - ", $teamString);
		return $teams;
	}
	// mass replace procents
	public function massReplace($proc)
	{
		$proc = str_replace("i1/G2.gif", "7", $proc);$proc = str_replace("i1/G1.gif", "5", $proc);$proc = str_replace("i1/B.gif", "4", $proc);//
		$proc = str_replace("i1/K2.gif", "4", $proc);$proc = str_replace("i1/K1.gif", "1", $proc);$proc = str_replace("i1/D.gif", "8", $proc);//
		$proc = str_replace("i1/L2.gif", "5", $proc);$proc = str_replace("i1/L1.gif", "7", $proc);$proc = str_replace("i1/E.gif", "5", $proc);//
		$proc = str_replace("i1/A2.gif", "1", $proc);$proc = str_replace("i1/A1.gif", "2", $proc);$proc = str_replace("i1/H.gif", "7", $proc);//
		$proc = str_replace("i1/H2.gif", "2", $proc);$proc = str_replace("i1/H1.gif", "9", $proc);$proc = str_replace("i1/L.gif", "9", $proc);//
		$proc = str_replace("i1/D2.gif", "6", $proc);$proc = str_replace("i1/D1.gif", "4", $proc);$proc = str_replace("i1/C.gif", "2", $proc);//
		$proc = str_replace("i1/C2.gif", "3", $proc);$proc = str_replace("i1/C1.gif", "0", $proc);$proc = str_replace("i1/A.gif", "0", $proc);//
		$proc = str_replace("i1/E2.gif", "0", $proc);$proc = str_replace("i1/E1.gif", "6", $proc);$proc = str_replace("i1/K.gif", "6", $proc);//
		$proc = str_replace("i1/B2.gif", "9", $proc);$proc = str_replace("i1/B1.gif", "8", $proc);$proc = str_replace("i1/J.gif", "1", $proc);//
		$proc = str_replace("i1/F2.gif", "8", $proc);$proc = str_replace("i1/F1.gif", "3", $proc);$proc = str_replace("i1/G.gif", "3", $proc);//
		$proc = str_replace("i1/M.gif", "", $proc);	 $proc = str_replace("i1/M.gif", "", $proc);  $proc = str_replace("i1/M.gif", "", $proc);//		
		return $proc;
	}
	// replace tips
	public function parseTip($tip)
	{
		$tip = str_replace("im/1B.gif", "1", $tip);
		$tip = str_replace("im/21.gif", "21", $tip);
		$tip = str_replace("im/X1.gif", "X1", $tip);
		$tip = str_replace("im/1X.gif", "1X", $tip);
		$tip = str_replace("im/2X.gif", "2X", $tip);
		$tip = str_replace("im/2B.gif", "2", $tip);
		$tip = str_replace("im/12.gif", "12", $tip);		
		return $tip;
	}
	// parse date from page
	public function parseDate($dateString)
	{		
		$dateString=trim($dateString);	
		return  $dateString;
	}
	
	// main function
    public function parseMainPage($page)
    {
    	
    	
    	// get source from $page
    	
    	$curl = curl_init();
    	curl_setopt($curl, CURLOPT_URL, $page);
    	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
    	$str = curl_exec($curl);
    	curl_close($curl);
    	$raw= str_get_html($str);
    	
    	// create object game
    	$gameTO = new GameTO();    	
    	//$gameModel = new Game();
    	
    	// get date from page 
    	$dateRaw = $raw->find('td[class=R5]');
    	$dayMonth = explode(" | ",$dateRaw[0]->innertext);
    	// find longest
    	$duzina =0;
    	$deo;
    	for ($i=0;$i<6;$i++){
    		if (strlen($dayMonth[$i])>$duzina){
    			$duzina=strlen($dayMonth[$i]);
    			$deo=$dayMonth[$i];
    		}
    	}
    	$rawData =  str_get_html($deo);
    	$deo=$rawData->plaintext;
    	$dateFromPage =$this->parseDate($deo);
    	
    	
    	// get parsing time    	
    	$date = new DateTime();
		$timeStamp = $date->format('Y-m-d H:i:s');
    	
    	
		
		// set counters
    	$rowNo=0;    	
    	$arrayOfGames;
    	$i=0;
    	
    	foreach($raw->find('tr[class=f2]') as $element){
    		
    		$gameTO->setDateFromPage($dateFromPage);
    		$gameTO->setTimestamp($timeStamp);
    		
    		// parsing teams   		
    		$teams = $this->parseTeams($element->children[1]->plaintext);
    		$gameTO->setHomeTeam($teams[0]);
    		$gameTO->setAwayTeam(trim($teams[1]));
    		
    		// parsing percents   		
    		$procenatFix1="";
    		if(array_key_exists('0', $element->children[2]->children)){
    			$proc = $element->children[2]->children[0]->src; 
    						
    			$proc=$this->massReplace($proc);
    			$procenatFix1 = $procenatFix1.$proc;
    			
    		}
    		else {
    			$procenatFix1="";
    			$proc = $element->children[2]->plaintext;      			
    			$procenatFix1 = $procenatFix1.$proc;
    			
    		}
    	
    		if(array_key_exists('1', $element->children[2]->children)){
    			$proc = $element->children[2]->children[1]->src. ' ';    		
    			$proc=$this->massReplace($proc);
    			$procenatFix1 = $procenatFix1.$proc;
    		}
    	
    		else {
    			$procenatFix1="";
    			$proc = $element->children[2]->plaintext;      			
    			$procenatFix1 = $procenatFix1.$proc;
    		}
    		
    		$procenatFix1=trim($procenatFix1);
    		$gameTO->setPercentFix1($procenatFix1);
    		// *************
    		
    		$procenatFixX="";
    		if(array_key_exists('0', $element->children[3]->children)){
    			$proc = $element->children[3]->children[0]->src;
    		
    			$proc=$this->massReplace($proc);
    			$procenatFixX = $procenatFixX.$proc;
    		}
    		else {
    			$procenatFixX='';
    			$proc = $element->children[3]->plaintext;  
    			$procenatFixX = $procenatFixX.$proc;
    		}
    		 
    		if(array_key_exists('1', $element->children[3]->children)){
    			$proc = $element->children[3]->children[1]->src. ' ';
    			$proc=$this->massReplace($proc);
    			$procenatFixX = $procenatFixX.$proc;
    		}
    		 
    		else {
    			$procenatFixX='';
    			$proc = $element->children[3]->plaintext;  
    			$procenatFixX = $procenatFixX.$proc;
    		}
    		
    		$procenatFixX=trim($procenatFixX);
    		$gameTO->setPercentFixX($procenatFixX);
    		// *************
    		
    		$procenatFix2="";
    		if(array_key_exists('0', $element->children[4]->children)){
    			$proc = $element->children[4]->children[0]->src;
    		
    			$proc=$this->massReplace($proc);
    			$procenatFix2 = $procenatFix2.$proc;
    		}
    		else {
    			$procenatFix2='';
    			$proc = $element->children[4]->plaintext;  
    			$procenatFix2 = $procenatFix2.$proc;
    		}
    		 
    		if(array_key_exists('1', $element->children[4]->children)){
    			$proc = $element->children[4]->children[1]->src. ' ';
    			$proc=$this->massReplace($proc);
    			$procenatFix2 = $procenatFix2.$proc;
    		}
    		 
    		else {
    			$procenatFix2='';
    			$proc = $element->children[4]->plaintext;  
    			$procenatFix2 = $procenatFix2.$proc;
    		}    		
    		$procenatFix2=trim($procenatFix2);
    		$gameTO->setPercentFix2($procenatFix2);
    		
    		// parsing of tips    		
    		$tip=$this->parseTip($element->children[5]->children[0]->src);
    		$gameTO->setTip($tip);
			// parsing odds
    		$odds1= $element->children[6]->plaintext;
    		$gameTO->setOdds1($odds1);
    		$oddsX= $element->children[7]->plaintext;
    		$gameTO->setOddsX($oddsX);
    		$odds2= $element->children[8]->plaintext;
    		$gameTO->setOdds2($odds2);
    		// other parse
    		$score1 = $element->children[9]->plaintext;
    		$gameTO->setScore1($score1);
    		$score2 = $element->children[10]->plaintext;
    		$gameTO->setScore2($score2);
    		$under = $element->children[11]->plaintext;
    		$gameTO->setUnder($under);
    		$over = $element->children[12]->plaintext;
    		$gameTO->setOver($over);
    		$result = $element->children[13]->plaintext;
    		$gameTO->setResult($result);
    		$gameTO->setRowNo($rowNo);  

    		//set league
    		$gameTO->setLeague($element->children[0]->plaintext);
    		
    		$rowNo++;
    		$arrayOfGames[$i]=$gameTO;
    		$gameTO= new GameTO();
    		$i++;
    		$procenatFix1="";
    		$procenatFixX="";
    		$procenatFix2="";
    			
    	}
    	
    	/////////////////////
    	$arrayOfF1=$raw->find('tr[class=f1]');
    	array_pop($arrayOfF1);
    	foreach($arrayOfF1 as $element){
    		
    		$gameTO->setDateFromPage($dateFromPage);
    		$gameTO->setTimestamp($timeStamp);
    		
    		// parsing teams   		
    		$teams = $this->parseTeams($element->children[1]->plaintext);
    		$gameTO->setHomeTeam($teams[0]);
    		$gameTO->setAwayTeam(trim($teams[1]));
    		
    		// parsing percents   		
    		$procenatFix1="";
    		if(array_key_exists('0', $element->children[2]->children)){
    			$proc = $element->children[2]->children[0]->src; 
    						
    			$proc=$this->massReplace($proc);
    			$procenatFix1 = $procenatFix1.$proc;
    			
    		}
    		else {
    			$procenatFix1="";
    			$proc = $element->children[2]->plaintext;    			
    			$procenatFix1 = $procenatFix1.$proc;
    			
    		}
    	
    		if(array_key_exists('1', $element->children[2]->children)){
    			$proc = $element->children[2]->children[1]->src. ' ';    		
    			$proc=$this->massReplace($proc);
    			$procenatFix1 = $procenatFix1.$proc;
    		}
    	
    		else {
    			$procenatFix1="";
    			$proc = $element->children[2]->plaintext;     			
    			$procenatFix1 = $procenatFix1.$proc;
    		}
    		
    		$procenatFix1=trim($procenatFix1);
    		$gameTO->setPercentFix1($procenatFix1);
    		// *************
    		
    		$procenatFixX="";
    		if(array_key_exists('0', $element->children[3]->children)){
    			$proc = $element->children[3]->children[0]->src;
    		
    			$proc=$this->massReplace($proc);
    			$procenatFixX = $procenatFixX.$proc;
    		}
    		else {
    			$procenatFixX='';
    			$proc = $element->children[3]->plaintext;  
    			$procenatFixX = $procenatFixX.$proc;
    		}
    		 
    		if(array_key_exists('1', $element->children[3]->children)){
    			$proc = $element->children[3]->children[1]->src. ' ';
    			$proc=$this->massReplace($proc);
    			$procenatFixX = $procenatFixX.$proc;
    		}
    		 
    		else {
    			$procenatFixX='';
    			$proc = $element->children[3]->plaintext;  
    			$procenatFixX = $procenatFixX.$proc;
    		}
    		
    		$procenatFixX=trim($procenatFixX);
    		$gameTO->setPercentFixX($procenatFixX);
    		// *************
    		
    		$procenatFix2="";
    		if(array_key_exists('0', $element->children[4]->children)){
    			$proc = $element->children[4]->children[0]->src;
    		
    			$proc=$this->massReplace($proc);
    			$procenatFix2 = $procenatFix2.$proc;
    		}
    		else {
    			$procenatFix2='';
    			$proc = $element->children[4]->plaintext;  
    			$procenatFix2 = $procenatFix2.$proc;
    		}
    		 
    		if(array_key_exists('1', $element->children[4]->children)){
    			$proc = $element->children[4]->children[1]->src. ' ';
    			$proc=$this->massReplace($proc);
    			$procenatFix2 = $procenatFix2.$proc;
    		}
    		 
    		else {
    			$procenatFix2='';
    			$proc = $element->children[4]->plaintext;  
    			$procenatFix2 = $procenatFix2.$proc;
    		}    		
    		$procenatFix2=trim($procenatFix2);
    		$gameTO->setPercentFix2($procenatFix2);
    		
    		// parsing of tips    		
    		$tip=$this->parseTip($element->children[5]->children[0]->src);
    		$gameTO->setTip($tip);
			// parsing odds
    		$odds1= $element->children[6]->plaintext;
    		$gameTO->setOdds1($odds1);
    		$oddsX= $element->children[7]->plaintext;
    		$gameTO->setOddsX($oddsX);
    		$odds2= $element->children[8]->plaintext;
    		$gameTO->setOdds2($odds2);
    		// other parse
    		$score1 = $element->children[9]->plaintext;
    		$gameTO->setScore1($score1);
    		$score2 = $element->children[10]->plaintext;
    		$gameTO->setScore2($score2);
    		$under = $element->children[11]->plaintext;
    		$gameTO->setUnder($under);
    		$over = $element->children[12]->plaintext;
    		$gameTO->setOver($over);
    		$result = $element->children[13]->plaintext;
    		$gameTO->setResult($result);
    		$gameTO->setRowNo($rowNo);   

    		//set league
    		$gameTO->setLeague($element->children[0]->plaintext);
    		
    		$rowNo++;
    		$arrayOfGames[$i]=$gameTO;
    		$gameTO= new GameTO();
    		$i++;
    		$procenatFix1="";
    		$procenatFixX="";
    		$procenatFix2="";
    		 
    	}    	
    	$raw->clear();
    	
    	return $arrayOfGames;
    	
    	
    }
}


?>